package org.androidpush.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import org.androidpush.R;
import org.androidpush.adapter.SelectCouAdpater;
import org.androidpush.client.LogUtil;
import org.androidpush.greendao.SelectCouInfo;
import org.androidpush.greendao.SelectCouInfoDao;
import org.androidpush.utils.ConstantUtils;
import org.androidpush.utils.DBUtils;

import java.util.List;

/**
 * Created by huang on 2017/4/16 0016.
 */
public class SelectCouActivity extends Activity {

    private static final String LOGTAG = LogUtil
            .makeLogTag(SelectCouActivity.class);

    private ListView couListView;
    private SelectCouInfoDao selectCouInfoDao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.xml_selectcou);
        init();
        ConstantUtils.aetActionBar(this, getString(R.string.str_course));
        setListViewItemOnClickListener();
    }

    private void init() {
        couListView = (ListView) findViewById(R.id.id_selectcou_list);
        selectCouInfoDao = DBUtils.getSelectCouInfoDao(this);
        String userId = ConstantUtils.getUserId(this);
        String url = ConstantUtils.URL + "JxhtWeb/CourseController/" + userId + "/getElectiveCourse";
        Log.d(LOGTAG, url);
        new SelectCouAsyncTask().execute(url);
    }

    class SelectCouAsyncTask extends
            AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            return ConstantUtils.getDateFrom(params[0]);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            List<SelectCouInfo> courseDtos = null;
            if (ConstantUtils.NET_ERROR.equals(result)) {
                if(selectCouInfoDao == null){
                    selectCouInfoDao = DBUtils.getSelectCouInfoDao(SelectCouActivity.this);
                }
                courseDtos = selectCouInfoDao.queryBuilder().list();
            } else {
                courseDtos = ConstantUtils.parseJsonArrayWithGson(result, SelectCouInfo.class);
                for(SelectCouInfo info:courseDtos){
                    selectCouInfoDao.insertOrReplace(info);
                }
            }
            if (courseDtos != null) {
                SelectCouAdpater adpater = new SelectCouAdpater(SelectCouActivity.this, courseDtos);
                couListView.setAdapter(adpater);
            }
        }
    }

    private void setListViewItemOnClickListener() {
        couListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView textView = (TextView) view.findViewById(R.id.id_selectcou_item_teacouId);
                String teaCouId = textView.getText().toString();
                Intent intent = new Intent();
                intent.setClass(SelectCouActivity.this, CouDetailActivity.class);
                intent.putExtra("teaCouId", teaCouId);
                startActivity(intent);
                SelectCouActivity.this.overridePendingTransition(R.anim.push_left_in,
                        R.anim.push_left_out);
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        DBUtils.closeDB();
    }
}
